package com.shoe.service.impl;

import com.shoe.mapper.OrderShoeMapper;
import com.shoe.mapper.ShoeCartMapper;
import com.shoe.mapper.ShoeOrderMapper;
import com.shoe.pojo.ShoeCartExample;
import com.shoe.pojo.ShoeOrder;
import com.shoe.pojo.ShoeOrderExample;
import com.shoe.pojo.ShoeOrderMoney;
import com.shoe.service.ShoeOrderService;
import com.shoe.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ShoeOrderServiceImpl implements ShoeOrderService {
    @Autowired
    ShoeOrderMapper shoeOrderMapper;

    @Autowired
    OrderShoeMapper orderShoeMapper;

    @Autowired
    ShoeCartMapper shoeCartMapper;

    @Override
    public int addOrder(ShoeOrder shoeOrder) {
       int a = shoeOrderMapper.insert(shoeOrder);
        ShoeCartExample shoeCartExample = new ShoeCartExample();
        shoeCartExample.createCriteria().andCartUserIdEqualTo(shoeOrder.getOrderUserId()).andCartGoodIdEqualTo(shoeOrder.getOrderGoodId());
        shoeCartMapper.deleteByExample(shoeCartExample);
        return a;
    }

    @Override
    public List<OrderVo> selectbyId(Long userId) {
        List<OrderVo> orderVo = orderShoeMapper.selectAllall(userId);



        return orderVo;
    }

    @Override
    public int updataDate(ShoeOrder shoeOrder) {

        ShoeOrderExample shoeOrderExample = new ShoeOrderExample();
        shoeOrderExample.createCriteria().andOrderNoEqualTo(shoeOrder.getOrderNo());
        int a = shoeOrderMapper.updateByExampleSelective(shoeOrder,shoeOrderExample);
        return a;
    }

    @Override
    public int selectByNo(ShoeOrder shoeOrder) {
        ShoeOrderExample shoeOrderExample = new ShoeOrderExample();
        shoeOrderExample.createCriteria().andOrderNoEqualTo(shoeOrder.getOrderNo());
        List<ShoeOrder> shoeOrders = shoeOrderMapper.selectByExample(shoeOrderExample);
        if(shoeOrders != null && shoeOrders.size()>0 ){
            return 1;
        }
        return 0;
    }

    @Override
    public List<ShoeOrder> selectByNo2(ShoeOrder shoeOrder) {
        ShoeOrderExample shoeOrderExample = new ShoeOrderExample();
        shoeOrderExample.createCriteria().andOrderNoEqualTo(shoeOrder.getOrderNo());
        List<ShoeOrder> shoeOrders = shoeOrderMapper.selectByExample(shoeOrderExample);
        return shoeOrders;
    }



    public List<ShoeOrder> selectAll() {
        List<ShoeOrder> list = shoeOrderMapper.selectByExample(null);
        return list;
    }

    public List<ShoeOrder> selectPage(Integer page, Integer limit) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("page",page);
        map.put("limit",limit);
        List<ShoeOrder> list = shoeOrderMapper.selectPage(map);
        return list;
    }

    public int updateOrder(ShoeOrder shoeOrder) {
        int a = shoeOrderMapper.updateByPrimaryKeySelective(shoeOrder);
        return a;
    }

    public int deleteOrder(Long orderId) {
        int a = shoeOrderMapper.deleteByPrimaryKey(orderId);
        return a;
    }

    public List<ShoeOrder> selectClassify(ShoeOrder shoeOrder, String time) {
        ShoeOrderExample shoeOrderExample = new ShoeOrderExample();
        Date date = new Date();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date begin = new Date();
        Calendar date1 = Calendar.getInstance();
        date1.setTime(begin);
        date1.set(Calendar.DATE, date1.get(Calendar.DATE) - Integer.parseInt(time));
        try {
            Date end = dft.parse(dft.format(date1.getTime()));
            shoeOrderExample.createCriteria().andOrderDateBetween(end,date);
            List<ShoeOrder> list = shoeOrderMapper.selectByExample(shoeOrderExample);
            return list;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<ShoeOrderMoney> selectOrderMoney(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date orderDate1 = format.parse(date1);
            Date orderDate2 = format.parse(date2);
            List<ShoeOrderMoney> list = shoeOrderMapper.selectOrderMoney(orderDate1,orderDate2);
            return list;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据orderID删除
    @Override
    public int deleteByorderNo(String orderNo) {
        ShoeOrderExample shoeOrderExample = new ShoeOrderExample();
        shoeOrderExample.createCriteria().andOrderNoEqualTo(orderNo);

       int a =  shoeOrderMapper.deleteByExample(shoeOrderExample);
        return a;
    }
}
